<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\7\17 0017
 * Time: 10:03
 */
namespace api;
use controller;
use model\goodsModel;

class goods extends controller{


    public function __construct($container)
    {
        parent::__construct($container);
//        header('Access-Control-Allow-Origin:*');
    }

    public function index(){


        $result = goodsModel::search();

        echo json_encode( $result );


    }

    public function get_list(){

    }

    public function single(){


//        $goods = new goodsModel($this->container);
//        $result = $goods->detail();

        $result = goodsModel::detail();



        echo json_encode( $result );

    }

    /**
     * 用来进行测试， 之后会删除掉
     */
    public function add(){

        $price = $_GET['price'];
        $name = $_GET['name'];
        $r = goodsModel::add(  $name, $price );

        $res = [
            'status' => 0,
            'description'=> '添加成功'
        ];

        //todo 添加到数据库

        echo json_encode( $res );

    }

    /**
     * 用来进行测试， 之后会删除掉
     */
    public function delete(){
        $res = [
            'status' => 0,
            'description'=> '删除成功'
        ];

        $id = $_GET['id'];

        $r = goodsModel::delete( $id );


        if( empty( $r->num_rows ) ){
            $res = [
                'status' => 1,
                'description'=> '删除失败没找到相关数据'
            ];
        }


        //todo 添加到数据库

        echo json_encode( $res );

    }
}